Beginning PHP by Unknown

Beginning PHP by Unknown
Author: Unknown
Language: eng
Format: mobi, epub
Publisher: Packt Publishing

Now, set up the following variables:$url = explode('/', trim($_SERVER['REQUEST_URI'], '/')); $controller = !empty($url[0]) ? $url[0] : $config['default_controller']; $method = !empty($url[1]) ? $url[1] : $config['default_method']; $args = !empty($url[2]) ? array_slice($url, 2) : array(); $class = $config['namespace'].$controller;0

Note


The $url will hold an array from the requested route in the form of /page/requested. This is how it works: When explode is run, it finds a forward slash in the requested URI, which the $_SERVER makes available.

Next, the $controller method uses a ternary operator to check if the 0 index of $url exists, otherwise the default_controller defined in the Config class is used.

The $method checks for the existence of a $url[1], otherwise it reads from the config class.

$args will get all other indexes of $url after the first 2.

$class holds the path to the controllers as set in the Config class.

What these parameters do is get the Controller, Method, and parameters from the requested URL. For instance:

http://localhost:8000/contacts/view/2

This results in:

Contacts = Contacts class.

View = View method inside the contacts class.

2 = A parameter passed to the method.

If the requested URL is http:://localhost:8000/, then no controller or method is requested, so the default controller and method will be used, as set in system\Config.php.